<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function parseParam(url) {
        // 提取? 后边的字符
        const paramsStr = /.+\?(.+)$/.exec(url)[1];
        const paramsArr = paramsStr.split("&");
        let paramsObj = {};
        paramsArr.forEach((param) => {
          // 处理有value 的参数
          if (/=/.test(param)) {
            let [key, val] = param.split("=");
            val = decodeURIComponent(val); //解码
            // 判断是否转为数字
            val = /^\d+$/.test(val) ? parseFloat(val) : val;
            if (paramsObj.hasOwnProperty(key)) {
              // 如果该属性存在 添加一个值
              paramsObj[key] = [].concat(paramsObj[key], val);
            } else {
              paramsObj[key] = val;
            }
          } else {
            paramsObj[key] = true;
          }
        });
        return paramsObj;
      }
    </script>
  </body>
</html>
